/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipartek.formacion.gestformwww.controlador;

import java.io.IOException;

import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Aratz Setién
 */
public class AccionServlet extends HttpServlet {

    private AccionFactory factory;

    public AccionFactory getFactory() {
        return factory;
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) {

        Accion action = null;
        AccionRouter router = null;
        try {
            action = factory.getAction(this.getNombreClaseRecurso(getNombreClase(request)), this.getClass().getClassLoader());
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(AccionServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(AccionServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(AccionServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        //FALTA COMPROBAR SI EL ROUTER ES NULL!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        router = action.ejecuta(this, request, response);
        try {

            router.enruta(this, request, response);
        } catch (ServletException ex) {
            Logger.getLogger(AccionServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(AccionServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private String getNombreClase(HttpServletRequest request) {
        String path = request.getServletPath();
        int barra = path.lastIndexOf("/");
        int punto = path.lastIndexOf(".");
        if (punto > 0 && punto > barra) {
            path = path.substring(barra + 1, punto);
        }
        return path;
    }

    public String getNombreClaseRecurso(String classNam) {
        String className = null;
        ResourceBundle bundle = (ResourceBundle) this.getServletContext().getAttribute("acciones-mapeo");
        className = bundle.getString(classNam);
        return className;
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        ResourceBundle bundle = null;
        factory = new AccionFactory();
        ServletContext sc = config.getServletContext();
        String recurso = config.getInitParameter("acciones-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("acciones-mapeo", bundle);
       
        recurso = config.getInitParameter("vistas-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("vistas-mapeo", bundle);

        recurso = config.getInitParameter("errores-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("errores-mapeo", bundle);
        
        recurso = config.getInitParameter("mensajes-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("mensajes-mapeo", bundle);

        recurso = config.getInitParameter("mailPlantillas-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("mailPlantillas-mapeo", bundle);

        recurso = config.getInitParameter("correo-mapeo");
        bundle = ResourceBundle.getBundle(recurso);
        sc.setAttribute("correo-mapeo", bundle);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Servlet Controlador del FRAMEWORK";
    }// </editor-fold>
}
